﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Pages.UserCenter
{
    public partial class GiveScore : CheckAuthPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.hidden);
                ltName.Text = AuthorityCenter.CurrentUserName;
                
            }
            if (AuthorityCenter.CurrentUserID > 0)
            {
                AccountsInfoBO acountbo = new AccountsInfoBO();
                AccountsInfo acount = acountbo.GetAcount(AuthorityCenter.CurrentUserID);
                UserInforBO inforbo = new UserInforBO();
                UserInfor infor = inforbo.GetInfo(AuthorityCenter.CurrentUserID);
                if (acount.MemberOrder <= 0 || acount.MemberOrder > 3)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "inforlogin", string.Format("alert('你不是会员,请开通会员!');document.location='{0}'; ", VirtualPathUtility.ToAbsolute("~/Pages/Shop/EquipPurchase.aspx")), true);
                    return;
                }
                else
                {
                    this.ltFortune.Text = infor != null ? infor.FortuneValue.ToString() : "0";
                    GameScoreInfoBO bo = new GameScoreInfoBO();
                    GameScoreInfo score = bo.GetGameScore(AuthorityCenter.CurrentUserID);
                    this.ltScore.Text = score != null ? score.Score.ToString() : "0";
                    this.ltInsurance.Text = score != null ? score.InsureScore.ToString() : "0";
                }
               
            }

        }
        public void SubGiveScore(object sender, EventArgs e)
        {
            try
            {
                string acountname = this.txtUserAcount.Text.Trim();
                string value = this.txtNumber.Text.Trim();
                string dbvalue = this.txtDBNumber.Text.Trim();
                if (value != dbvalue)
                {
                    ShowCurError("两次输入数量不一致");
                    return;
                }
                int givescore = 0;
                if (!int.TryParse(value, out givescore))
                {
                    ShowCurError("积分格式不正确，请输入整数。");
                    return;
                }
                if (givescore < 2500)
                {
                    ShowCurError("最低赠送2500堂豆。");
                    return;
                }

                AccountsInfoBO acountbo = new AccountsInfoBO();
                AccountsInfo acount = acountbo.GetLightAcountInfor(acountname);
                if (acount == null)
                {
                    this.txtUserAcount.Text = "";
                    ShowCurError("该会员不存在");
                    return;
                }

                if (acount.UserID == AuthorityCenter.CurrentUserID)
                {
                    ShowCurError("不可以赠送给自己");
                    return;
                }
                GameScoreLockerBO lockbo = new GameScoreLockerBO();
                if (lockbo.GetLocker(acount.UserID) != null)
                {
                    ShowCurError("对方会员在游戏房间中，不能赠送积分。");
                    return;
                }
                if (lockbo.GetLocker(AuthorityCenter.CurrentUserID) != null)
                {
                    ShowCurError("会员在游戏房间中，不能赠送积分。");
                    return;
                }


                GameScoreInfoBO gamebo = new GameScoreInfoBO();
                GameScoreInfo score = gamebo.GetGameScore(AuthorityCenter.CurrentUserID);
                double currentscore = score != null ? score.Score : 0;
                if (currentscore < givescore)
                {
                    ShowCurError("用户堂豆数量不够");
                    return;
                }
                score.Score -= givescore;
                gamebo.SaveGameScore(score);

                GameScoreInfo targetscore = gamebo.GetGameScore(acount.UserID);
                if (targetscore == null)
                {
                    targetscore = new GameScoreInfo();
                    targetscore.Score = givescore;
                    targetscore.Revenue = 0;
                    targetscore.InsureScore = 0;
                    targetscore.WinCount = 0;
                    targetscore.LostCount = 0;
                    targetscore.DrawCount = 0;
                    targetscore.FleeCount = 0;
                    targetscore.UserRight = 0;
                    targetscore.AllLogonTimes = 0;
                    targetscore.PlayTimeCount = 0;
                    targetscore.UserID = acount.UserID;
                    targetscore.OnLineTimeCount = 0;

                    targetscore.RegisterIP = string.IsNullOrEmpty(acount.RegisterIP) ? "1" : acount.RegisterIP;
                    targetscore.RegisterDate = (acount.RegisterDate == DateTime.MinValue ? DateTime.Now : acount.RegisterDate);
                    targetscore.LastLogonDate =(acount.LastLogonDate == DateTime.MinValue ? DateTime.Now : acount.LastLogonDate);
                    targetscore.LastLogonIP = string.IsNullOrEmpty(acount.LastLogonIP) ? "2" : acount.LastLogonIP;

                    gamebo.AddGameScore(targetscore);
                }
                else
                {
                    targetscore.Score += givescore;
                    gamebo.SaveGameScore(targetscore);
                }
                cftt_zsjfBO zs = new cftt_zsjfBO();
                cftt_zsjf zsrecord = new cftt_zsjf();
                zsrecord.createdate = DateTime.Now;
                zsrecord.fromuser = AuthorityCenter.CurrentUserName;
                zsrecord.touser = acount.Accounts;
                zsrecord.jf = givescore;
                zs.AddGiveScore(zsrecord);
                this.ltScore.Text = score.Score.ToString();
                ShowCurError("赠送成功!");
            }
            catch
            {
                ShowCurError("服务器异常!");
            }
        }
    }
}